const format = require("../../utils/format.js")

Page({

  data: {
    dateTime: "",
    code: "",
    isLoading: false,
    isSuccess: false
  },

  /**
   * 获取登录Code
   */
  getLoginCode: function () {
    if (this.data.isLoading) {
      return;
    }

    this.setData({ isLoading: true })

    let that = this;

    wx.login({
      success: (res) => {
        if (res.code) {
          that.setData({
            code: res.code,
            isSuccess: true,
            dateTime: format.dateTimeFormat(new Date())
          })
        } else {
          that.setData({ code: "Code获取失败", isSuccess: false })
        }
      },
      fail: () => {
        that.setData({ code: "Code获取失败", isSuccess: false })
      },
      complete: () => {
        that.setData({ isLoading: false })
      }
    })
  },

  /**
   * 复制Code到剪贴板
   */
  copyCode: function () {
    if (!this.data.isSuccess) {
      wx.showToast({
        title: "Code不存在",
        icon: "error",
        mask: true
      })

      return;
    }

    let that = this;
    wx.setClipboardData({
      data: that.data.code,
    })
  }
})